<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Redirection {

    function index() {
        $CI = &get_instance();
        $enable_redirect = TRUE;
        $active_maintenance = FALSE;
        $CI->load->library('user_agent');


        if($active_maintenance AND !is_admin() AND $CI->uri->segment(3) != 'maintenance') {
            if($enable_redirect)
                redirect('home/maintenance');
            else
                fred('maintenance');
        }
        else if(!$active_maintenance) {

            // Admin
            if(!is_admin() AND $CI->layout == 'admin') {
                if($enable_redirect) {
                    set_flashdata(lang('message_no_admin_access'));
                    redirect('home');
                }
                else
                    fred('admin');
            }

            // IE redirection
            if($CI->uri->segment(3) != 'IE' AND ($CI->agent->is_browser('MSIE') OR $CI->agent->is_browser('Internet Explorer'))) {
                if($enable_redirect)
                    redirect('home/IE');
                else
                    fred('IE');
            }

            // Auth
            if(!is_logged_in() AND $CI->layout != 'blank') {
                if($enable_redirect) {
                    set_flashdata(lang('message_unlogged'));
                    redirect('home');
                }
                else
                    fred('auth');
            }

            // CHPP authorize
            if(is_logged_in() AND !get_user_profile('valid_token') AND $CI->uri->segment(2) != 'sync') {
                if($enable_redirect)
                    redirect('sync/authorize');
                else
                    fred('sync');
            }

            // Main
            if(is_logged_in() AND $CI->layout == 'blank') {
                if($enable_redirect)
                    redirect('home/main');
                else
                    fred('main');
            }

            // Language
            if(is_logged_in() AND get_user_profile('language') != NULL AND get_user_profile('language') != $CI->uri->segment(1)) {
                if($enable_redirect)
                    redirect(get_user_profile('language').'/home/main');
                else
                    fred('lang');
            }
        }

    }

}